<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no" />
    <title>首页</title>
    <link rel="stylesheet" type="text/css" href="../c/aui.css" />
</head>

<body>
</body>
<script type="text/javascript" src="../j/api.js"></script>
<script src="../j/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
    var UIPhotoViewer, index = 0,
        lists = [];
    apiready = function() {
		urls();
        lists = api.pageParam['img'] ? [fenge(api.pageParam['img'])] : api.pageParam['list'];
    //    lists = ['fs://touxiang.png']
        UIPhotoViewer = api.require('UIPhotoViewer');
        api.parseTapmode();
        img();
    }
    function fenge(a){
      a = a.split('?')[0];
      return a;
    }

    function img() {
  //      console.log(JSON.stringify(lists));
        UIPhotoViewer.open({
            images: lists,
            bgColor: '#000',
            gestureClose: false,
            activeIndex:api.pageParam['index'] ? api.pageParam['index'] : 0
        }, function(ret, err) {
            if (ret) {
                index = ret.index
            //    console.log(JSON.stringify(ret));
                if (ret.eventType == 'click') {
                    _close();
                }
                if (ret.eventType == 'show') {
                    api.openFrame({
                        name: 'img_footer',
                        url: _h_w + 'img_footer.html',
                        rect: {
                            x: 0,
                            y: 0, // 这里传win的高是为了打开后先不显示Frame，让Frame自己设置自己的高
                            w: 'auto',
                            h: 0
                        },
                        pageParam:api.pageParam,
                        bgColor: 'rgba(0,0,0,0.0)',
                    });
                }
            } else {
                console.log(JSON.stringify(err));
            }
        });
    }
    // 下载图片
    function baocun() {
      _loading('保存中...')
      UIPhotoViewer.getImage({
          index: index
      }, function(ret, err) {
          if (ret) {
            var img = ret.path;
            if(img){
              if(api.systemType == 'ios'){
                api.saveMediaToAlbum({
                    path: img,
                    groupName:'www_suxiangw_com'
                }, function(ret, err) {
                  console.log(JSON.stringify(ret))
                  _loadingCloes();
                   if(ret){
                     console.log(JSON.stringify(ret));
                     if(ret.status){
                      _msg('保存成功，请到相册查看')
                     }
                   }else{
                     console.log(JSON.stringify(err));
                    _alert('保存失败')
                   }
                });
              }else{
                download();
              }
            }else{
              _loadingCloes();
              _alert('图片读取失败')
            }
          } else {
            _loadingCloes();
            console.log(JSON.stringify(err));
          }
      });
    }
    // 下载保存图片
    function download(){
      api.download({
			    url: lists[index],
			    savePath: 'fs://www_suxiangw_com_'+_file_name()+'.png',
			    report: true,
			    cache: true,
			    allowResume: true
			}, function(ret, err) {
					console.log(JSON.stringify(ret)+JSON.stringify(err))
			    if (ret) {
						if(ret.state == 1){
              console.log(ret.savePath)
							api.saveMediaToAlbum({
							    path:ret.savePath,
							    // groupName:'www_suxiangw_com'
							}, function(ret, err) {
								_loadingCloes();
						    if (ret && ret.status) {
                  _msg('保存成功，请到相册查看')
						    } else {
							  _alert('保存失败');
						    }
							});
						}else if(ret.state == 2){
							_loadingCloes();
							_alert('下载失败');
						}else{
							console.log('下载进度：'+ret.percent);
						}
			    } else {
				  	_alert(err.msg);
						_loadingCloes();
			    }
			});


    }
</script>

</html>
